package com.wzh.musichome.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzh.musichome.common.ConstantCode;
import com.wzh.musichome.common.ResponseTemplate;
import com.wzh.musichome.entity.dto.ListSongDTO;
import com.wzh.musichome.entity.pojo.ListSong;
import com.wzh.musichome.entity.pojo.Song;
import com.wzh.musichome.service.ListSongService;
import com.wzh.musichome.service.SongService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 歌单包含歌曲列表 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2020-12-10
 */
@RestController
@RequestMapping("/listSong")
public class ListSongController {
    @Autowired
    private ListSongService listSongService;

    @Autowired
    private SongService songService;

    /**
     * 给歌单添加歌曲
     */
    @PostMapping("/add")
    public Object addListSong(ListSongDTO listSongDTO){

        ListSong listSong = new ListSong();
        BeanUtils.copyProperties(listSongDTO,listSong);
        try {
//            QueryWrapper<ListSong> wrapper = new QueryWrapper<>();
//            wrapper.eq("name",listSongDTO.ge);
//            songService.getOne()
            boolean b = listSongService.save(listSong);
            if (b) {
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("新增成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "新增失败！");
        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_INSERT, "新增失败！！ 具体原因" + e.getMessage());
        }
    }

    /**
     * 根据歌单id查询歌曲
     */
   @GetMapping("/detail")
    public Object detail(@RequestParam int songListId){
       try {
           QueryWrapper<ListSong> wrapper = new QueryWrapper<>();
           wrapper.eq("song_list_id",songListId);
           List<ListSong> list = listSongService.list(wrapper);
           ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
           data.setMsg("查找成功");
           data.setData(list);
           return data;
       } catch (Exception e) {
           return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_QUERY, "查找失败！！ 具体原因" + e.getMessage());

       }
   }

    /**
     * 删除歌单里的歌曲
     */
    @GetMapping("/delete")
    public Object delete(@RequestParam int songId,@RequestParam int songListId){
        QueryWrapper<ListSong> wrapper = new QueryWrapper<>();
        wrapper.eq("song_list_id",songListId)
                .eq("song_id",songId);
        try {
            boolean b = listSongService.remove(wrapper);
            if (b){
                ResponseTemplate.ResultData data = ResponseTemplate.getSuccessResponse();
                data.setMsg("删除成功");
                return data;
            }
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！");

        } catch (Exception e) {
            return ResponseTemplate.getFailureResponse(ConstantCode.FAILURE_DELETE, "删除失败！！ 具体原因" + e.getMessage());
        }
    }

}
